package Demo1;

class Student {
    String name;
    int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}


class Person {
    private String name;
    private String gender;
    int age;
    // 想要让该类支持深拷贝，覆写Object类的clone方法即可
}


public class test1 {
    public static int getElement(int[] array, int index) {
        if (null == array) {
            throw new NullPointerException("传递的数组为null");
            //抛出空指针异常
        }
        if (index < 0 || index >= array.length) {
            throw new ArrayIndexOutOfBoundsException("传递的数组下标越界");
            //数组越界异常
        }
        return array[index];
    }


    public static void main(String[] args) {
        int[] array = {1,2,3};
        getElement(array, 3);
    }

    public static void main11(String[] args) {
        int[] arr = null;
        System.out.println(arr.length);
    }

    public static void main10(String[] args) {
        int[] arr = {1, 2, 3};
        System.out.println(arr[100]);
    }

    public static void main9(String[] args) {
        System.out.println(10 / 0);
    }

    public static void main8(String[] args) {
        String s1 = "hello world!!!";
        System.out.println(s1.substring(5));
        System.out.println(s1.substring(3, 9));
    }

    public static void main7(String[] args) {
        String s1 = "I am a good boy!";
        String[] arr1 = s1.split(" ");
        String[] arr2 = s1.split(" ", 2);
        for (int i = 0; i < arr1.length; i++) {
            System.out.println(arr1[i]);
        }
        System.out.println();
        for (int i = 0; i < arr2.length; i++) {
            System.out.println(arr2[i]);
        }
    }

    public static void main6(String[] args) {
        String s1 = "aaabbbcccdddaaabbbcccddd";
        String s2 = s1.replaceFirst("aaa", "zzz");
        String s3 = s1.replaceAll("aaa", "vvv");
        System.out.println(s1);
        //原字符串没有改变
        System.out.println(s2);
        //replaceFirst只修改第一个
        System.out.println(s3);
        //replaceAll修改所有出现的
    }

    public static void main5(String[] args) {
        String s = String.format("%d-%d-%d", 2019, 9, 14);
        String s1 = String.format("%c + %c + %s", 'l', 's', "xn");
        System.out.println(s1);
        //有种c语言的感觉
        System.out.println(s);
    }

    public static void main4(String[] args) {
        String s1 = "abcdefg";
        char[] array = s1.toCharArray();
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
    }

    public static void main3(String[] args) {
        String s1 = "abcDEF";
        String s2 = "ABCdef";
        String s3 = s1.toLowerCase();
        String s4 = s2.toUpperCase();
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
        System.out.println(s4);

    }

    public static void main2(String[] args) {
        String s1 = "123456";
        String s2 = "123.456";

        int num1 = Integer.valueOf(s1);
        double num2 = Double.valueOf(s2);

        System.out.println(num1);
        System.out.println(num2);
    }


    public static void main1(String[] args) {
        String s1 = String.valueOf(1234);
        String s2 = String.valueOf(12.34);
        String s3 = String.valueOf(true);
        String s4 = String.valueOf(new Student("zhangsan", 18));
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
        System.out.println(s4);
        //可以将很多类型转化为字符串
        //甚至是一个自定义的类 只要重写了toString方法就可以做到
    }
}
